﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserControls
{
    public partial class UCCreateMessage : System.Web.UI.UserControl
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Page.Request.Params["Type"] != null)
                {
                    if (Page.Request.Params["Type"].ToString() == "Create")
                        btnCreate.Text = "Create";
                    else
                        btnCreate.Text = "Update";

                }
               
                SetDataSourceToBranches(structureFacade.GetBranches(""));
                SetDataSourceToUnits(structureFacade.GetUnit(""));
                SetDataSourceToFrom(structureFacade.GetUserType(""));
                SetDataSourceToSoCode(structureFacade.GetUser(""));

                DisableControlsForHQ();
            }

        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

            //CreateMessage();
            UpdateMessage();
        }

        public string Heading
        {
            get
            {
                return lblHeading.Text;
            }
            set
            {
                lblHeading.Text = (string)value;
                lblHeading.Visible = true;

            }

        }

        public string SubHeading
        {
            get
            {
                return lblSubHeading.Text;
            }
            set
            {
                lblSubHeading.Text = (string)value;

            }

        }

        public string ButtonText
        {
            get
            {
                return btnCreate.Text;
            }
            set
            {
                btnCreate.Text = (string)value;

            }

        }

        public void SetDataSourceToBranches(List<Branch> branches )
        {
            ddlBranch.DataSource = branches;           
            ddlBranch.DataTextField = "BranchName";
            ddlBranch.DataValueField = "BranchID";
            ddlBranch.DataBind();

            InsertDDLItems(ddlBranch,"Select...",0);
            InsertDDLItems(ddlBranch, "Select All", 1);
        }

        public void SetDataSourceToUnits(List<SalesOneUnit> units)
        {
            ddlUnit.DataSource = units;
            ddlUnit.DataTextField = "Name";
            ddlUnit.DataValueField = "UnitId";
            ddlUnit.DataBind();

            InsertDDLItems(ddlUnit, "Select...", 0);
            InsertDDLItems(ddlUnit, "Select All", 1);
        }

        public void SetDataSourceToSoCode(List<User> users)
        {
            ddlSOCode.DataSource = users;
            ddlSOCode.DataTextField = "Name";
            ddlSOCode.DataValueField = "UserId";
            ddlSOCode.DataBind();

            InsertDDLItems(ddlSOCode, "Select...", 0);
            InsertDDLItems(ddlSOCode, "Select All", 1);
        }

        public void SetDataSourceToFrom(List<UserType> userTypes)
        {
            ddlFrom.DataSource =userTypes;
            ddlFrom.DataTextField = "UserTypeName";
            ddlFrom.DataValueField = "UserTypeId";
            ddlFrom.DataBind();

            InsertDDLItems(ddlFrom, "Select...", 0);
            InsertDDLItems(ddlFrom, "Select All", 1);
        }

        public void CreateMessage()
        {          
            BusinessObjects.Message message =new IronOne.SalesOne.BusinessObjects.Message();

            message.MessageBody = txtMessage.Text;
            message.MessageFromUserTypeID =Convert.ToInt32( ddlFrom.SelectedValue);
            //message.CreatedBy = "";
            message.Created = DateTime.Now;
            message.CreatedBy = new Guid("fa0714ba-8c04-4a75-9b82-a87766179bc1");
            try
            {
                message.SoCodeId = Convert.ToInt32(ddlSOCode.SelectedValue);
            }
            catch { }

            if (chkBroadcast.Checked)
            {
                message.UserTypeId = 1;
            }

            if (ddlSOCode.SelectedIndex != 0)
            {
                //message.UserTypeId =;
                message.NodeId = -1;
            }
            else if (ddlUnit.SelectedIndex != 0)
            {
                message.UserTypeId = 4;
                message.NodeId = Convert.ToInt32(ddlUnit.SelectedValue);
            }
            else if (ddlBranch.SelectedIndex != 0)
            {
                message.UserTypeId = 3;
                message.NodeId = Convert.ToInt32(ddlBranch.SelectedValue);
            }
           
            Facade.MessageFacade messageFacade = new Facade.MessageFacade();
            messageFacade.InsertMessage(message);

           
        }

        public void UpdateMessage()
        {
            BusinessObjects.Message message = new IronOne.SalesOne.BusinessObjects.Message();
            message.Message_ID = 4;
            message.MessageBody = txtMessage.Text;
            try
            {
                message.MessageFromUserTypeID = Convert.ToInt32(ddlFrom.SelectedValue);
            }
            catch { message.MessageFromUserTypeID = 1; }
            //message.CreatedBy = "";
            message.Created = DateTime.Now;
            message.CreatedBy = new Guid("fa0714ba-8c04-4a75-9b82-a87766179bc1");
            try
            {
                //message.SoCodeId = Convert.ToInt32(ddlSOCode.SelectedValue);
                message.SoCodeId = 222;
            }
            catch { }

            if (chkBroadcast.Checked)
            {
                message.UserTypeId = 1;
            }

            if (ddlSOCode.SelectedIndex != 0)
            {
                //message.UserTypeId =;
                message.NodeId = -1;
            }
            else if (ddlUnit.SelectedIndex != 0)
            {
                message.UserTypeId = 4;
                message.NodeId = Convert.ToInt32(ddlUnit.SelectedValue);
            }
            else if (ddlBranch.SelectedIndex != 0)
            {
                message.UserTypeId = 3;
                message.NodeId = Convert.ToInt32(ddlBranch.SelectedValue);
            }
            message.UserTypeId = 1;
            Facade.MessageFacade messageFacade = new Facade.MessageFacade();
            int a= messageFacade.UpdateMessage(message);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void ddlFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisableControlsForHQ();
        }

        protected void chkBroadcast_CheckedChanged(object sender, EventArgs e)
        {
            DisableControlsForBroadcasting();
        }

        public void DisableControlsForHQ()
        {
            if (ddlFrom.SelectedItem.Text.Equals("HQ", StringComparison.InvariantCultureIgnoreCase))
                chkBroadcast.Enabled = true;
            else
            {
                chkBroadcast.Enabled = false;
                chkBroadcast.Checked = false;
                DisableControlsForBroadcasting();
            }

        }

        public void DisableControlsForBroadcasting()
        {
            if (chkBroadcast.Checked)
            {
                ddlBranch.Enabled = false;
                ddlUnit.Enabled = false;
                ddlSOCode.Enabled = false;
            }
            else
            {
                ddlBranch.Enabled = true;
                ddlUnit.Enabled = true;
                ddlSOCode.Enabled = true;
            }
        }

        public void InsertDDLItems(DropDownList ddl,string Text,int Position)
        {
            ddl.Items.Insert(Position,Text);
        }
    }
}